﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Cocbook_DTO;
using Cocbook_DAL;

namespace Sale_and_manage
{
    public partial class frmBillHistory : DevComponents.DotNetBar.Metro.MetroForm
    {
        public frmBillHistory()
        {
            InitializeComponent();
            LoadProjectName();
            LoadNewdgvBillHistory();
        }

        public List<Project> lpr = new List<Project>();

        public BillDAL billDAL = new BillDAL();

        public void LoadProjectName()
        {
            ProjectDAL pDAL = new ProjectDAL();
            lpr = pDAL.GetAllProject();
            foreach (Project pr in lpr)
            {
                cboProjectName.Items.Add(pr.ProjectName);
            }
            
        }

        public int GetProjectIndex(Project p)
        {
            int result = 0;
            for (int i = 0; i < lpr.Count; i++)
            {
                if (lpr[i].ProjectID == p.ProjectID)
                {
                    result = i;
                    break;
                }
            }
            return result;
        }

        public void LoadNewdgvBillHistory()
        {
            List<Bill> lbi = new List<Bill>();
            ProjectDAL pDAL = new ProjectDAL();
            Project p = pDAL.GetProjectByStatus();
            cboProjectName.SelectedIndex = GetProjectIndex(p);
            lbi = billDAL.GetAllBillByProjectID(p.ProjectID);
            List<BillView> lbv = new List<BillView>();
            BillView bv;
            if (lbi != null)
            {
                for (int i = 0; i < lbi.Count; i++)
                {
                    bv = new BillView(lbi[i]);
                    lbv.Add(bv);
                }
                dgvBillHistory.DataSource = lbv;
                dgvBillHistory.Columns[0].Width = 40;
                dgvBillHistory.Columns[1].Width = 70;
                dgvBillHistory.Columns[2].Width = 90;
                dgvBillHistory.Columns[3].Width = 70;
                dgvBillHistory.Columns[4].Width = 120;
                dgvBillHistory.Columns[5].Width = 70;
                dgvBillHistory.Columns[6].Width = 70;
                dgvBillHistory.Columns[7].Width = 90;
                superTabControl1.SelectedTab = tabSearch;
                dgvBillHistory.Rows[0].Selected = true;
            }
            else
            {
                dgvBillHistory.DataSource = new List<Bill_BookView>();
                dgvBillHistory.Columns[0].Width = 40;
                dgvBillHistory.Columns[1].Width = 70;
                dgvBillHistory.Columns[2].Width = 90;
                dgvBillHistory.Columns[3].Width = 70;
                dgvBillHistory.Columns[4].Width = 120;
                dgvBillHistory.Columns[5].Width = 70;
                dgvBillHistory.Columns[6].Width = 70;
                dgvBillHistory.Columns[7].Width = 90;
            }
        }

        public void LoadBillByProject()
        {
            List<Bill> lbi = new List<Bill>();
            lbi = billDAL.GetAllBillByProjectID(lpr[cboProjectName.SelectedIndex].ProjectID);
            List<BillView> lbv = new List<BillView>();
            BillView bv;
            for (int i = 0; i < lbi.Count; i++)
            {
                bv = new BillView(lbi[i]);
                lbv.Add(bv);
            }
            dgvBillHistory.DataSource = lbv;
            dgvBillHistory.Columns[0].Width = 40;
            dgvBillHistory.Columns[1].Width = 70;
            dgvBillHistory.Columns[2].Width = 90;
            dgvBillHistory.Columns[3].Width = 70;
            dgvBillHistory.Columns[4].Width = 120;
            dgvBillHistory.Columns[5].Width = 70;
            dgvBillHistory.Columns[6].Width = 70;
            dgvBillHistory.Columns[7].Width = 90;
            superTabControl1.SelectedTab = tabSearch;
            dgvBillHistory.ClearSelection();
        }

        public void LoadOrder()
        {
            List<Order> lor = new List<Order>();
            OrderDAL oDAL = new OrderDAL();
            lor = oDAL.GetAllOrder();
            if (lor != null)
            {
                List<OrderView> lov = new List<OrderView>();
                OrderView ov;
                for (int i = 0; i < lor.Count; i++)
                {
                    ov = new OrderView(lor[i]);
                    lov.Add(ov);
                }
                dgvBillHistory.DataSource = lov;
            }
            else
            {
                dgvBillHistory.DataSource = new List<OrderView>();
            }
            dgvBillHistory.Columns[0].Width = 50;
            dgvBillHistory.Columns[1].Width = 75;
            dgvBillHistory.Columns[2].Width = 75;
            dgvBillHistory.Columns[3].Width = 150;
            dgvBillHistory.Columns[4].Width = 65;
            dgvBillHistory.Columns[5].Width = 65;
            dgvBillHistory.Columns[6].Width = 75;
            dgvBillHistory.Columns[7].Width = 85;
            dgvBillHistory.ClearSelection();

        }

        private void cboProjectName_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearInfo();
            if (lpr[cboProjectName.SelectedIndex].ProjectName == "Order")
            {
                LoadOrder();
                txtStaffID.Enabled = false;
                dgvDetail.DataSource = new List<Order_BookView>();
            }
            else
            {
                LoadBillByProject();
                txtStaffID.Enabled = true;
                dgvDetail.DataSource = new List<Bill_BookView>();
            }
        }

        public void LoadBillDetail(int row)
        {
            List<Bill_Book> lbb = new List<Bill_Book>();
            Bill_BookDAL bbDAL = new Bill_BookDAL();
            lbb = bbDAL.GetBill_BookByBillID(Convert.ToInt16(dgvBillHistory[0, row].Value));
            if (lbb != null)
            {
                List<Bill_BookView> lbbv = new List<Bill_BookView>();
                Bill_BookView bbv;
                for (int i = 0; i < lbb.Count; i++)
                {
                    bbv = new Bill_BookView(lbb[i]);
                    lbbv.Add(bbv);
                }
                dgvDetail.DataSource = lbbv;
                dgvDetail.Columns[0].Width = 90;
                dgvDetail.Columns[1].Width = 120;
                dgvDetail.Columns[2].Width = 50;
                dgvDetail.Columns[3].Width = 60;
                dgvDetail.Columns[4].Width = 60;
            }
            else
            {
                dgvDetail.DataSource = new List<Bill_BookView>();
            }
        }

        public void LoadOrderDetail(int row)
        {
            List<Order_Book> lob = new List<Order_Book>();
            Order_BookDAL obDAL = new Order_BookDAL();
            lob = obDAL.GetOrder_BookByOrderID(Convert.ToInt16(dgvBillHistory[0, row].Value));
            if (lob != null)
            {
                List<Order_BookView> lobv = new List<Order_BookView>();
                Order_BookView obv;
                for (int i = 0; i < lob.Count; i++)
                {
                    obv = new Order_BookView(lob[i]);
                    lobv.Add(obv);
                }
                dgvDetail.DataSource = lobv;
                dgvDetail.Columns[0].Width = 90;
                dgvDetail.Columns[1].Width = 120;
                dgvDetail.Columns[2].Width = 50;
                dgvDetail.Columns[3].Width = 60;
                dgvDetail.Columns[4].Width = 60;
            }
            else
            {
                dgvDetail.DataSource = new List<Order_BookView>();
            }
        }


        private void dgvBillHistory_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (cboProjectName.Text != "Order")
                {
                    LoadBillDetail(e.RowIndex);
                    lblID.Text = dgvBillHistory[0, e.RowIndex].Value.ToString();
                    lblCustomerID.Text = dgvBillHistory[2, e.RowIndex].Value.ToString();
                    if (Convert.ToInt32(dgvBillHistory[2, e.RowIndex].Value) != 0)
                    {
                        CustomerDAL cdal = new CustomerDAL();
                        lblCustomerName.Text = cdal.GetCustomerByCMND(Convert.ToInt32(dgvBillHistory[2, e.RowIndex].Value)).CName;
                    }
                    lblDatetime.Text = dgvBillHistory[4, e.RowIndex].Value.ToString();
                    lblTotal.Text = dgvBillHistory[5, e.RowIndex].Value.ToString();

                }
                else
                {
                    LoadOrderDetail(e.RowIndex);
                    lblID.Text = dgvBillHistory[0, e.RowIndex].Value.ToString();
                    lblCustomerID.Text = dgvBillHistory[1, e.RowIndex].Value.ToString();
                    CustomerDAL cdal = new CustomerDAL();
                    lblCustomerName.Text = cdal.GetCustomerByCMND(Convert.ToInt32(dgvBillHistory[1, e.RowIndex].Value)).CName;
                    lblDatetime.Text = dgvBillHistory[2, e.RowIndex].Value.ToString();
                    lblTotal.Text = dgvBillHistory[4, e.RowIndex].Value.ToString();
                }
            }
        }

        private void frmBillHistory_Resize(object sender, EventArgs e)
        {
            if (cboProjectName.Text == "Order")
            {
                dgvBillHistory.Columns[3].Width = 150 + this.Size.Width - 1030;
            }
            else
            {
                dgvBillHistory.Columns[0].Width = 40 + (this.Size.Width-1030)/8;
                dgvBillHistory.Columns[1].Width = 70 + (this.Size.Width - 1030) / 8;
                dgvBillHistory.Columns[2].Width = 90 + (this.Size.Width - 1030) / 8;
                dgvBillHistory.Columns[3].Width = 70 + (this.Size.Width - 1030) / 8;
                dgvBillHistory.Columns[4].Width = 120 + (this.Size.Width - 1030) / 8;
                dgvBillHistory.Columns[5].Width = 70 + (this.Size.Width - 1030) / 8;
                dgvBillHistory.Columns[6].Width = 70 + (this.Size.Width - 1030) / 8;
                dgvBillHistory.Columns[7].Width = 90 + (this.Size.Width - 1030) / 8;
            }
        }

        public void FilterBillHistory()
        {
            List<Bill> lbi = new List<Bill>();
            DateTime date = new DateTime();
            if (cboDate.Value != date)
            {
                date = cboDate.Value;
            }
            else
            {
                date = DateTime.Parse("1/1/1800");
            }
            int cid = 0;
            if (txtCustomerID.Text != "") cid = int.Parse(txtCustomerID.Text);
            int bid = 0;
            if (txtBillID.Text != "") bid = int.Parse(txtBillID.Text);
            int sid = 0;
            if (txtStaffID.Text != "") sid = int.Parse(txtStaffID.Text); 
            int ftotal = 0;
            if (txtLowPrice.Text != "") ftotal = int.Parse(txtLowPrice.Text);
            int ltotal = 0;
            if (txtHighPrice.Text != "") ltotal = int.Parse(txtHighPrice.Text);
            lbi = billDAL.GetAllBillByFilter(lpr[cboProjectName.SelectedIndex].ProjectID, bid, cid, sid, date, ftotal, ltotal);
            List<BillView> lbv = new List<BillView>();
            BillView bv;
            for (int i = 0; i < lbi.Count; i++)
            {
                bv = new BillView(lbi[i]);
                lbv.Add(bv);
            }
            dgvBillHistory.DataSource = lbv;
        }

        public void FilterOrderHistory()
        {
            List<Order> lor = new List<Order>();
            DateTime date = new DateTime();
            OrderDAL orderDAL = new OrderDAL();
            if (cboDate.Value != date)
            {
                date = cboDate.Value;
            }
            else
            {
                date = DateTime.Parse("1/1/1800");
            }
            int cid = 0;
            if (txtCustomerID.Text != "") cid = int.Parse(txtCustomerID.Text);
            int bid = 0;
            if (txtBillID.Text != "") bid = int.Parse(txtBillID.Text);
            int sid = 0;
            if (txtStaffID.Text != "") sid = int.Parse(txtStaffID.Text);
            int ftotal = 0;
            if (txtLowPrice.Text != "") ftotal = int.Parse(txtLowPrice.Text);
            int ltotal = 0;
            if (txtHighPrice.Text != "") ltotal = int.Parse(txtHighPrice.Text);
            lor = orderDAL.GetAllOrderByFilter(bid, cid,date, ftotal, ltotal);
            List<OrderView> lov = new List<OrderView>();
            OrderView ov;
            for (int i = 0; i < lor.Count; i++)
            {
                ov = new OrderView(lor[i]);
                lov.Add(ov);
            }
            dgvBillHistory.DataSource = lov;
        }

        public string ShowID(int id)
        {
            string result = "0000000" + id.ToString();
            return result.Substring(result.Length - 7);
        }

        public void ClearInfo()
        {
            lblID.Text = "";
            lblTotal.Text = "";
            lblDatetime.Text = "";
            lblCustomerName.Text = "";
            lblCustomerID.Text = "";
        }

        private void tabEdit_Click(object sender, EventArgs e)
        {
            if (lblID.Text == "")
            {
                superTabControl1.SelectedTab = tabSearch;
                MessageBox.Show("Bạn phải chọn hóa đơn trước");
            }
            else
            {
                if (cboProjectName.Text == "Order")
                {
                    lblID.Text = ShowID(Convert.ToInt32(dgvBillHistory.SelectedRows[0].Cells[0].Value));
                    chkTranffered.Checked = Convert.ToBoolean(dgvBillHistory.SelectedRows[0].Cells[6].Value);
                    txtShipCost.Text = dgvBillHistory.SelectedRows[0].Cells[5].Value.ToString();
                    pnlOrder.Visible = true;
                    dgvBillHistory.Enabled = false;
                    cboProjectName.Enabled = false;
                }
                else
                {
                    lblID.Text = ShowID(Convert.ToInt32(dgvBillHistory.SelectedRows[0].Cells[0].Value));
                    pnlBill.Visible = true;
                    dgvBillHistory.Enabled = false;
                    cboProjectName.Enabled = false;
                }
            }
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            { 
                case Keys.Enter:
                    btnFilter_Click(null, null);
                    break;
            }

            return base.ProcessDialogKey(keyData);
        }

        private void tabSearch_Click(object sender, EventArgs e)
        {
            cboProjectName.Enabled = true;
            dgvBillHistory.Enabled = true;
            if (cboProjectName.Text == "Order")
            {
                pnlOrder.Visible = false;
            }
            else
            {
                pnlBill.Visible = false;
            }
        }


        private void btnClear_Click(object sender, EventArgs e)
        {
            if (cboProjectName.Text == "Order")
            {
                LoadOrder();
            }
            else
            {
                LoadBillByProject();
            }
            txtBillID.Clear();
            txtLowPrice.Clear();
            txtCustomerID.Clear();
            txtHighPrice.Clear();
            txtStaffID.Clear();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (cboProjectName.Text != "Order")
            {
                Bill oBill;
                oBill = billDAL.GetBillByID(Convert.ToInt16(lblID.Text));
                CustomerDAL cDAL = new CustomerDAL();
                Customer c = cDAL.GetCustomerByCMND(Convert.ToInt32(txtEditCustomerID.Text));
                if (c != null)
                {
                    if (oBill.Customer.CustomerID == 1)
                    {
                        DialogResult r = MessageBox.Show("Bạn có muốn thay đổi người sở hữu hóa đơn này thành " + c.CName + " hay không?", "Thay đổi thông tin hóa đơn", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (r == DialogResult.Yes)
                        {
                            c.Points = c.Points + Convert.ToInt32(oBill.Total);
                            oBill.Customer = c;
                            billDAL.UpdateBill(oBill);
                            cDAL.UpdateCustomer(c);
                            txtCustomerID.Clear();
                            tabSearch.Focus();
                        }
                        else
                        {
                            txtCustomerID.Focus();
                            txtCustomerID.SelectAll();
                        }
                    }
                    else
                    {
                        DialogResult r = MessageBox.Show(c.CName+" đang sở hữu hóa đơn này. Bạn có muốn thay đổi người sở hữu hóa đơn này thành " + c.CName + " hay không?", "Thay đổi thông tin hóa đơn", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (r == DialogResult.Yes)
                        {
                            c.Points = c.Points + Convert.ToInt32(oBill.Total);
                            oBill.Customer = c;
                            billDAL.UpdateBill(oBill);
                            cDAL.UpdateCustomer(c);
                            txtCustomerID.Clear();
                            tabSearch.Focus();
                        }
                        else
                        {
                            txtCustomerID.Focus();
                            txtCustomerID.SelectAll();
                        }
                    }
                }
                else
                {
                    txtEditCustomerID.Focus();
                    txtEditCustomerID.SelectAll();
                    highlighter1.SetHighlightColor(txtEditCustomerID, DevComponents.DotNetBar.Validator.eHighlightColor.Red);
                }
            }
            else
            {
                
                OrderDAL orderDAL = new OrderDAL();
                Order oOrder = orderDAL.GetOrderByID(Convert.ToInt32(lblID.Text));
                oOrder.Transfered = chkTranffered.Checked;
                oOrder.ShipCost = Convert.ToDouble(txtShipCost.Text);
                MessageBox.Show("Lưu thành công");
            }
        }

        private void txtShipCost_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar) &&
                !((e.KeyChar == ',') && !txtShipCost.Text.Contains(",") && !txtShipCost.Text.Contains(".")) &&
                !((e.KeyChar == '.') && !txtShipCost.Text.Contains(",") && !txtShipCost.Text.Contains(".")))
            {
                e.Handled = true;
            }
        }

        private void txtEditCustomerID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            if (cboProjectName.Text != "Order")
            {
                FilterBillHistory();
            }
        }

        private void btnReprint_Click(object sender, EventArgs e)
        {

        }

    }
}